package com.alm.web.controller.alm.devbaseinfo;


import com.alm.common.core.domain.AjaxResult;
import com.alm.common.exception.ServiceException;
import com.alm.handle.core.domain.entity.rq.DevGroupEditRq;
import com.alm.handle.core.domain.entity.rq.DevGroupListRq;
import com.alm.handle.core.domain.entity.rq.DevGroupAddRq;
import com.alm.handle.core.domain.entity.vo.DevGroupDetailVO;
import com.alm.handle.core.domain.entity.vo.DevGroupDictVO;
import com.alm.handle.core.domain.entity.vo.DevInfoListVO;
import com.alm.handle.service.AlmDevGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 设备分组信息表 前端控制器
 * </p>
 *
 * @author ywy
 * @since 2024-06-12
 */
@RestController
@RequestMapping("/alm/dev_group")
public class AlmDevGroupController {

    @Resource
    private AlmDevGroupService almDevGroupService;

    /**
     * 分组列表
     * @param rq
     * @return
     */
    @PreAuthorize("@ss.hasRole('common')")
    @PostMapping("/list")
    public AjaxResult getDevGroupList(@RequestBody DevGroupListRq rq) {
        return almDevGroupService.getDevGroupList(rq);
    }

    /**
     * 新增分组
     * @param rq
     * @return
     */
    @PreAuthorize("@ss.hasRole('common')")
    @PostMapping("/add")
    public AjaxResult addDevGroup(@RequestBody DevGroupAddRq rq) {
        if(StringUtils.isBlank(rq.getGroupName().trim())) {
            throw new ServiceException("分组名不可为空！");
        }
        int row = almDevGroupService.addDevGroup(rq);
        return row == 1 ? AjaxResult.success() : AjaxResult.error("新增失败");
    }

    /**
     * 更新分组
     * @param rq
     * @return
     */
    @PreAuthorize("@ss.hasRole('common')")
    @PostMapping("/edit")
    public AjaxResult editDevGroup(@RequestBody DevGroupEditRq rq) {
        if(StringUtils.isBlank(rq.getGroupName().trim())) {
            throw new ServiceException("分组名不可为空！");
        }
        int row = almDevGroupService.editDevGroup(rq);
        return row == 1 ? AjaxResult.success() : AjaxResult.error("更新失败");
    }

    /**
     * 分组信息详情
     * @param devGroupId
     * @return
     */
    @PreAuthorize("@ss.hasRole('common')")
    @GetMapping("/detail/{devGroupId}")
    public AjaxResult detail(@PathVariable("devGroupId") @NotNull(message = "id不可为空")  Integer devGroupId) {
        DevGroupDetailVO vo = almDevGroupService.detail(devGroupId);
        return AjaxResult.success(vo);
    }

    /**
     * 删除分组信息
     * @param devGroupId
     * @return
     */
    @PreAuthorize("@ss.hasRole('common')")
    @GetMapping("/del/{devGroupId}")
    public AjaxResult del(@PathVariable("devGroupId") @NotNull(message = "id不可为空") Integer devGroupId){
        int row = almDevGroupService.del(devGroupId);
        return AjaxResult.success();
    }

    /**
     * 获取设备分组下拉框
     * @return
     */
    @PreAuthorize("@ss.hasRole('common')")
    @GetMapping("/devGroupDict")
    public AjaxResult getDevGroupDict() {
        List<DevGroupDictVO> list = almDevGroupService.getDict();
        return AjaxResult.success(list);
    }

    /**
     * 获取分组下的设备信息
     * @param devGroupId
     * @return
     */
    @PreAuthorize("@ss.hasRole('common')")
    @GetMapping("/getDevInfoByGroupId/{devGroupId}")
    public AjaxResult getDevInfoByGroupId(@PathVariable("devGroupId") @NotNull(message = "id不可为空") Integer devGroupId) {
        List<DevInfoListVO> list = almDevGroupService.getDevInfoByGroupId(devGroupId);
        return AjaxResult.success(list);
    }

}

